/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2014</p>
 * @author prayer
 * @version 1.0
 */
package com.cms.pub;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.dict.FCTServiceMgr;
import com.efounder.eai.data.JParamObject;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2014</p>
 * @author prayer
 * @version 1.0
 */
public class EMPSCTZUtils {
	public static void checkBMItemEdit(EFRowSet headRowSet, EFDataSet itemDataSet, EFRowSet nodeRowSet,FCTServiceMgr FCTService) {
		EFDataSet     flowDataSet = null;
		EFRowSet       itemRowSet = null;
		JParamObject       flowPO = JParamObject.Create();
		String         BILL_WHERE = "";
		
		BILL_WHERE = " BIZ_DATE = '" + headRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + headRowSet.getString("F_DJBH", "") + "' and FLOW_ID = 'SCTZ_FLOW'";
		flowPO.SetValueByParamName("tableName", "FLOW_TASK_END_LIST");
		flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
		flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		flowDataSet = FCTService.getFCTRows(flowPO);
		//如果有数据，则表示流程已经结束,所有部门信息分录都不可修改
		if(flowDataSet.getRowCount() > 0) {
			for(int i = 0; i < itemDataSet.getRowCount(); i++) {
				itemRowSet = itemDataSet.getRowSet(i);
				itemRowSet.putString("EDIT", "0");
			}
			return;
		} 

		BILL_WHERE = " BIZ_DATE = '" + headRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + headRowSet.getString("F_DJBH", "") + "' and FLOW_ID = 'SCTZ_FLOW' and "
		           + " NODE_TAG = '" + nodeRowSet.getString("NODE_ID", "") + "' and RESR_STATUS = 'pending' and (F_CHAR19 = '' or F_CHAR19 = ' ' or F_CHAR19 is null)"
		           + " and (TASK_TO_UNIT = '' or TASK_TO_UNIT = ' ' or TASK_TO_UNIT is null)";
		flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
		flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
		flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		flowDataSet = FCTService.getFCTRows(flowPO);
		//F_CHAR19和TASK_TO_UNIT为空，说明不是会签节点，可以修改
		if(flowDataSet.getRowCount() > 0) {
			for(int i = 0; i < itemDataSet.getRowCount(); i++) {
				itemRowSet = itemDataSet.getRowSet(i);
				itemRowSet.putString("EDIT", "1");
			}
			return;
		} 
		
		//流程没有结束
		for(int i = 0; i < itemDataSet.getRowCount(); i++) {
			itemRowSet = itemDataSet.getRowSet(i);
			//如果没有节点信息，则不能修改部门信息，一般情况下都有节点信息，以防万一
			if(nodeRowSet == null) {
				itemRowSet.putString("EDIT", "0");
			} 
			//如果不是开始节点，则不能修改部门信息
			else if(!nodeRowSet.getString("NODE_TYPE", "").equals("start")) {
				itemRowSet.putString("EDIT", "0");
			} else {
				//分两种情况，可以编辑
				//部门退回，TASK_TO_UNIT为空，F_CHAR19为退回部门编号
				//部门退回后，在第一个节点又增加了新的部门
				//新建单据
				
				//1、查看该部门是否在流程节点中，如果没有，则有两种情况，第一种是新建单据；第二种是部门退回后，又添加了新的部门，这两种情况不会在节点流程中，则可以编辑
				BILL_WHERE = " BIZ_DATE = '" + itemRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + itemRowSet.getString("F_DJBH", "") + "' and "
		                   + " FLOW_ID = 'SCTZ_FLOW' and RESR_STATUS = 'pending' and TASK_TO_UNIT = '" + itemRowSet.getString("F_BMBH", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				if(flowDataSet.getRowCount() > 0) {
					itemRowSet.putString("EDIT", "0");
					continue;
				} 
				
				//2、部门退回
				BILL_WHERE = " BIZ_DATE = '" + itemRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + itemRowSet.getString("F_DJBH", "") + "' and "
		                   + " FLOW_ID = 'SCTZ_FLOW' and RESR_STATUS = 'pending' and F_CHAR19 = '" + itemRowSet.getString("F_BMBH", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				//如果有数据，则说明是新添加的
				if(flowDataSet.getRowCount() > 0) {
					itemRowSet.putString("EDIT", "1");
				} else {
					itemRowSet.putString("EDIT", "0");
					continue;
				}
				itemRowSet.putString("EDIT", "1");
			}
		}
	}
	
	public static void checkWXTZBMItemEdit(EFRowSet headRowSet, EFDataSet itemDataSet, EFRowSet nodeRowSet,FCTServiceMgr FCTService) {
		EFDataSet     flowDataSet = null;
		EFRowSet       itemRowSet = null;
		JParamObject       flowPO = JParamObject.Create();
		String         BILL_WHERE = "";
		
		BILL_WHERE = " BIZ_DATE = '" + headRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + headRowSet.getString("F_DJBH", "") + "' and FLOW_ID = 'WXTZ_FLOW'";
		flowPO.SetValueByParamName("tableName", "FLOW_TASK_END_LIST");
		flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
		flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		flowDataSet = FCTService.getFCTRows(flowPO);
		//如果有数据，则表示流程已经结束,所有部门信息分录都不可修改
		if(flowDataSet.getRowCount() > 0) {
			for(int i = 0; i < itemDataSet.getRowCount(); i++) {
				itemRowSet = itemDataSet.getRowSet(i);
				itemRowSet.putString("EDIT", "0");
			}
			return;
		} 

		BILL_WHERE = " BIZ_DATE = '" + headRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + headRowSet.getString("F_DJBH", "") + "' and FLOW_ID = 'WXTZ_FLOW' and "
		           + " NODE_TAG = '" + nodeRowSet.getString("NODE_ID", "") + "' and RESR_STATUS = 'pending' and (F_CHAR19 = '' or F_CHAR19 = ' ' or F_CHAR19 is null)"
		           + " and (TASK_TO_UNIT = '' or TASK_TO_UNIT = ' ' or TASK_TO_UNIT is null)";
		flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
		flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
		flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		flowDataSet = FCTService.getFCTRows(flowPO);
		//F_CHAR19和TASK_TO_UNIT为空，说明不是会签节点，可以修改
		if(flowDataSet.getRowCount() > 0) {
			for(int i = 0; i < itemDataSet.getRowCount(); i++) {
				itemRowSet = itemDataSet.getRowSet(i);
				itemRowSet.putString("EDIT", "1");
			}
			return;
		} 
		
		//流程没有结束
		for(int i = 0; i < itemDataSet.getRowCount(); i++) {
			itemRowSet = itemDataSet.getRowSet(i);
			//如果没有节点信息，则不能修改部门信息，一般情况下都有节点信息，以防万一
			if(nodeRowSet == null) {
				itemRowSet.putString("EDIT", "0");
			} 
			//如果不是开始节点，则不能修改部门信息
			else if(!nodeRowSet.getString("NODE_TYPE", "").equals("start")) {
				itemRowSet.putString("EDIT", "0");
			} else {
				//分两种情况，可以编辑
				//部门退回，TASK_TO_UNIT为空，F_CHAR19为退回部门编号
				//部门退回后，在第一个节点又增加了新的部门
				//新建单据
				
				//1、查看该部门是否在流程节点中，如果没有，则有两种情况，第一种是新建单据；第二种是部门退回后，又添加了新的部门，这两种情况不会在节点流程中，则可以编辑
				BILL_WHERE = " BIZ_DATE = '" + itemRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + itemRowSet.getString("F_DJBH", "") + "' and "
		                   + " FLOW_ID = 'WXTZ_FLOW' and RESR_STATUS = 'pending' and TASK_TO_UNIT = '" + itemRowSet.getString("F_BMBH", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				if(flowDataSet.getRowCount() > 0) {
					itemRowSet.putString("EDIT", "0");
					continue;
				} 
				
				//2、部门退回
				BILL_WHERE = " BIZ_DATE = '" + itemRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + itemRowSet.getString("F_DJBH", "") + "' and "
		                   + " FLOW_ID = 'WXTZ_FLOW' and RESR_STATUS = 'pending' and F_CHAR19 = '" + itemRowSet.getString("F_BMBH", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				//如果有数据，则说明是新添加的
				if(flowDataSet.getRowCount() > 0) {
					itemRowSet.putString("EDIT", "1");
				} else {
					itemRowSet.putString("EDIT", "0");
					continue;
				}
				itemRowSet.putString("EDIT", "1");
			}
		}
	}
}
